package com.wawei.seeds.services;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.wawei.seeds.model.HeroInfo;
import com.wawei.seeds.services.remote.HeroInfoRestfulWrapper;
import com.wawei.seeds.services.remote.ProviderInfoRestfulWrapper;
import com.wawei.seeds.services.remote.WalnutRegAuthRestfulService;
import com.wawei.seeds.utils.NetBase;
import com.wawei.seeds.utils.PrefUtils;
import com.wawei.seeds.utils.RetrofitNetworkService;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.mime.TypedFile;

/**
 *
 * Created by t27891 on 15/7/2.
 */
public class WalnutPortraitServiceHandler implements ServiceHandler {
    public static final String PORTRAIT_WHO = "who";
    public static final String OLD_PASSWORD = "old";
    public static final String NEW_PASSWORD = "new";

    private Context context;
    public WalnutPortraitServiceHandler(Context context) {
        this.context = context;
    }

    @Override
    public void retrieveData(RestAdapter restAdapter, Bundle bundle, final Handler handler, final Integer handlerTag) {
        int whoisit = bundle.getInt(PORTRAIT_WHO);

        WalnutRegAuthRestfulService service = restAdapter.create(WalnutRegAuthRestfulService.class);
        service.getHeroInfo(Integer.valueOf(whoisit), new Callback<ProviderInfoRestfulWrapper>() {
            @Override
            public void success(ProviderInfoRestfulWrapper heroInfoRestfulWrapper, Response response) {
                List<HeroInfo> heros = new ArrayList<>(0);
                heros.add(heroInfoRestfulWrapper.getProviderBean());
                RetrofitNetworkService.handleSuccess(handler, handlerTag, heros.toArray());
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                RetrofitNetworkService.handleError(handler, handlerTag, retrofitError);
            }
        });
    }

    public void uploadHeader(RestAdapter restAdapter, final Handler handler, Integer userId, TypedFile file, String fileName, String fileType, final Integer handlerTag) {
        WalnutRegAuthRestfulService service = restAdapter.create(WalnutRegAuthRestfulService.class);
        service.uploadHeader(userId, file, fileName, fileType, new Callback<HeroInfoRestfulWrapper>() {
            @Override
            public void success(HeroInfoRestfulWrapper result, Response response) {
                try {
                    /*
                    if (netBase.getResultCode() == 200) {
                        RetrofitNetworkService.handleSuccess(handler, handlerTag, "ok");
                    } else {
                        RetrofitNetworkService.handleError(handler, handlerTag, null);
                    }
                    */
                    if (result.getResultCode() == 200) {
                        RetrofitNetworkService.handleSuccess(handler, handlerTag, result.getPictureUrl());
                    } else {
                        RetrofitNetworkService.handleError(handler, handlerTag, null);
                    }
                } catch (Exception e) {
                    RetrofitNetworkService.handleError(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, null);
            }
        });
    }

    public void changeMyPassword(RestAdapter restAdapter, Bundle bundle, final Handler handler, final Integer handlerTag){
        WalnutRegAuthRestfulService service = restAdapter.create(WalnutRegAuthRestfulService.class);
        service.changePassword(String.valueOf(PrefUtils.getPrefUser(context)), bundle.getString(OLD_PASSWORD), bundle.getString(NEW_PASSWORD), new Callback<NetBase>() {
            @Override
            public void success(NetBase netBase, Response response) {
                if(netBase.getResultCode() == RetrofitNetworkService.FAILED) {
                    RetrofitNetworkService.handleError(handler, handlerTag, null);
                }
                else {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                RetrofitNetworkService.handleError(handler, handlerTag, retrofitError);
            }
        });
    }
}
